<?php

namespace App\Repositories\Eloquent;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use App\Repositories\BaseRepository;

abstract class EloquentBaseRepository implements BaseRepository
{
    /**
     * @var \Illuminate\Database\Eloquent\Model An instance of the Eloquent Model
     */
    protected $model;
    
        /**
         * @param Model $model
         */
        public function __construct($model)
        {
            $this->model = $model;
        }
    
        /**
         * @inheritdoc
         */
        public function find($id, $with = [])
        {

            if(!empty($with)) {
                return $this->model->with($with)->find($id);
            }
            return $this->model->find($id);
        }


    
        /**
         * @inheritdoc
         */
        public function all()
        {
            // if (method_exists($this->model, 'translations')) {
            //     return $this->model->orderBy('created_at', 'DESC')->get();
            // }
    
            return $this->model->orderBy('created_at', 'DESC')->get();
        }
    
        /**
         * @inheritdoc
         */
        public function allWithBuilder() : Builder
        {
            // if (method_exists($this->model, 'translations')) {
            //     return $this->model;
            // }
    
            return $this->model;
        }
    
        /**
         * @inheritdoc
         */
        public function paginate($perPage = 15, $with = [])
        {
            $model = $this->model;
            if (!empty($with)){
                $model = $model::with($with);
            }
            // if (method_exists($this->model, 'translations')) {
            //     return $this->model->orderBy('created_at', 'DESC')->paginate($perPage);
            // }
    
            return $model->orderBy('created_at', 'DESC')->paginate($perPage);
        }

        /**
         * @inheritdoc
         */
        public function paginateWithCondition($perPage = 15, $with = [], $condition=[])
        {
            $model = $this->model;
            if (!empty($with)){
                $model = $model::with($with);
            }
            if (!empty($condition)) {
                foreach ($condition as $k => $v) {
                    if (is_array($v) && in_array($v[0], ['like','>', '<']) ){
                        $model = $model->where($k, $v[0], $v[1]);
                    } else {
                        $model = $model->where($k, $v);
                    }
                }
            }
            // if (method_exists($this->model, 'translations')) {
            //     return $this->model->orderBy('created_at', 'DESC')->paginate($perPage);
            // }

            return $model->orderBy('created_at', 'DESC')->paginate($perPage);
        }
    
        /**
         * @inheritdoc
         */
        public function create($data)
        {
            return $this->model->create($data);
        }
    
        /**
         * @inheritdoc
         */
        public function update($model, $data)
        {
            $model->update($data);
    
            return $model;
        }
    
        /**
         * @inheritdoc
         */
        public function destroy($model)
        {
            return $model->delete();
        }
    
        /**
         * @inheritdoc
         */
        // public function allTranslatedIn($lang)
        // {
        //     return $this->model->whereHas('translations', function (Builder $q) use ($lang) {
        //         $q->where('locale', "$lang");
        //     })->orderBy('created_at', 'DESC')->get();
        // }
    
        /**
         * @inheritdoc
         */
        public function findByWhere($slug,$value , $with=[])
        {

            return $this->model->with($with)->where($slug,$value)->first();
        }
    
        /**
         * @inheritdoc
         */
        public function findByAttributes(array $attributes)
        {
            $query = $this->buildQueryByAttributes($attributes);
    
            return $query->first();
        }
    
        /**
         * @inheritdoc
         */
        public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
        {
            $query = $this->buildQueryByAttributes($attributes, $orderBy, $sortOrder);
    
            return $query->get();
        }
    
        /**
         * Build Query to catch resources by an array of attributes and params
         * @param  array $attributes
         * @param  null|string $orderBy
         * @param  string $sortOrder
         * @return \Illuminate\Database\Eloquent\Builder
         */
        private function buildQueryByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
        {
            $query = $this->model->query();
    
            // if (method_exists($this->model, 'translations')) {
            //     $query = $query;
            // }
    
            foreach ($attributes as $field => $value) {
                $query = $query->where($field, $value);
            }
    
            if (null !== $orderBy) {
                $query->orderBy($orderBy, $sortOrder);
            }
    
            return $query;
        }
    
        /**
         * @inheritdoc
         */
        public function findByMany(array $ids)
        {
            $query = $this->model->query();
    
            // if (method_exists($this->model, 'translations')) {
            //     $query = $query;
            // }
    
            return $query->whereIn("id", $ids)->get();
        }
    
        /**
         * @inheritdoc
         */
        public function clearCache()
        {
            return true;
        }
}
